package net.goutalk.glcs.module.bi.service.impl;

import cn.hutool.db.DbUtil;
import cn.hutool.db.ds.simple.SimpleDataSource;
import com.baomidou.mybatisplus.annotation.DbType;
import net.goutalk.glcs.common.exception.MyException;
import net.goutalk.glcs.common.utils.DatasourceUtil;
import net.goutalk.glcs.module.bi.dto.TestDbDto;
import net.goutalk.glcs.module.bi.entity.VisualDb;
import net.goutalk.glcs.module.bi.mapper.VisualDbMapper;
import net.goutalk.glcs.module.bi.service.IVisualDbService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * BI 数据源表 服务实现类
 * </p>
 *
 * @author zjq
 * @since 2022-09-21
 */
@Service
public class VisualDbServiceImpl extends ServiceImpl<VisualDbMapper, VisualDb> implements IVisualDbService {

    @Override
    public boolean testConnectionDb(TestDbDto dto) {
        try (SimpleDataSource ds = new SimpleDataSource(dto.getUrl(), dto.getUserName(), dto.getPassword(), dto.getDriverClass())) {
            DbUtil.use(ds).query(DatasourceUtil.wrapperTestSql(DbType.getDbType(dto.getDbType())));
        } catch (Exception e) {
            throw new MyException("链接失败");
        }
        return true;
   }

}
